package com.example.application.views.change;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.security.PermitAll;

import com.example.application.compose.BadgeBasic;
import com.example.application.compose.BadgeIcons;
import com.example.application.compose.Button_Deal;
import com.example.application.compose.NumericField;
import com.example.application.views.MainLayout;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.dom.ThemeList;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.theme.lumo.Lumo;

@Route(value = "", layout = MainLayout.class)
@PageTitle("更改深浅色")
@PermitAll
// @JsModule("./clipboard.min.js")
public class ChangeView extends VerticalLayout {
    private List<String> items = new ArrayList<>(
            Arrays.asList("Chrome", "Edge", "Firefox", "Safari"));
    private int count=0;
    public ChangeView() {
        // UI.getCurrent().getPage().addJsModule("/js/animation.js", LoadMode.LAZY);
        TextField textField = new TextField();
        textField.setLabel("输入地址");
        textField.setHelperText("帮助文本");
        textField.setMinWidth("40em");
        // textField.setValue("Ruukinkatu 2");
        textField.setPlaceholder("输入");
        textField.setClearButtonVisible(true);
        textField.setPrefixComponent(VaadinIcon.MAP_MARKER.create());
        textField.addInputListener(event->{
            System.out.println("hi"+count++);
            // System.out.println(((TextField)event.getSource()).getMaxLength());
        });
        add(textField);
        Button_Deal button_Deal=new Button_Deal("标签", "复制");
        add(button_Deal);
        
        BadgeBasic bd=new BadgeBasic();
        add(bd);
        BadgeIcons bdic=new BadgeIcons();
        add(bdic);
        NumericField num=new NumericField(10,5,-10);
        add(num);
        
        H1 h1 = new H1("你好,Darkness");
        Button toggleButton = new Button("Toggle dark theme", click -> {
            ThemeList themeList = UI.getCurrent().getElement().getThemeList(); 

            if (themeList.contains(Lumo.DARK)) { 
                themeList.remove(Lumo.DARK);
            } else {
                themeList.add(Lumo.DARK);
            }
        });
        ComboBox<String> comboBox = new ComboBox<>("DIY");
        comboBox.setAllowCustomValue(true);
        comboBox.addCustomValueSetListener(e -> {
            String customValue = e.getDetail();
            items.add(customValue);
            comboBox.setItems(items);
            // comboBox.setValue(customValue);
        });
        comboBox.setItems(items);
        comboBox.setHelperText("Select or type a Browser");
        add(comboBox);
        toggleButton.getElement().setAttribute("theme", Lumo.DARK);
        add(toggleButton, h1);
    }

    protected void onAttach(AttachEvent attachEvent) {
        UI ui = getUI().get();
        Button button = new Button("Logout", event -> {
            // Redirect this page immediately
            ui.getPage().executeJs("window.location.href='logout.html'");

            // Close the session
            ui.getSession().close();
        });

        add(button);

        // Notice quickly if other UIs are closed
        ui.setPollInterval(3000);
    }
    
}
